//#include "stdafx.h"
#include "TreeStatistic.h"

CTreeStatistic::CTreeStatistic(void)
{
}

CTreeStatistic::~CTreeStatistic(void)
{
}

int	CTreeStatistic::InitAverageDist()
{
	DistCount	= 0;
	SumDist		= 0;

	return	1;
}

int	CTreeStatistic::MakeAverageDist(CHiBoostImageEntity &entity, NodeInstanceType &inst, double &AvePos)
{
	double		dx, dy;
	double		dist;
	int			LeafID;

	dist	= 0;
	for ( int i = 0; i < (int) inst.LeafNodeInfo.size(); i++ )
	{
		LeafID	= inst.LeafNodeInfo[i].LeafNodeIndex;
		dx		= inst.LeafNodeInfo[i].node.x - entity.ParsedLeafInstanceList.pElement[LeafID].layer_fx;
		dy		= inst.LeafNodeInfo[i].node.y - entity.ParsedLeafInstanceList.pElement[LeafID].layer_fy;
		//dx		= inst.LeafNodeInfo[i].node.x - entity.List_PtrParsedLeafInstance.pElement[LeafID]->layer_fx;
		//dy		= inst.LeafNodeInfo[i].node.y - entity.List_PtrParsedLeafInstance.pElement[LeafID]->layer_fy;
		dist	+= sqrt(dx*dx + dy*dy);
	}

	AvePos	= dist / (int) inst.LeafNodeInfo.size();
	SumDist	+= AvePos;
	DistCount++;
	return	1;
}

int	CTreeStatistic::PrintSearchStatInfo(string strSearchStatFileName)
{
	fstream		outSearchStat;

	outSearchStat.open(strSearchStatFileName.c_str(), ios_base::app);

	SumDist	/= DistCount;
	outSearchStat.precision(3);
	outSearchStat<<"Average Distance: "<<setw(12)<<fixed<<SumDist;
	outSearchStat<<" ("<<setw(4)<<fixed<<DistCount<<")"<<endl;

	outSearchStat.close();
	outSearchStat.clear();
	return	1;
}